/*初始化datagrid*/
$(function () {
    /*抽取常用组件*/
    var emp_list = $("#emp_list");
    var emp_dialog = $("#emp_dialog");
    var emp_form = $("#emp_form");
    /*对象管理方法*/
    var object = {
        check: function () {
            var row = emp_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择您要查看的保单~', 'info');
            } else {
                emp_form.form('clear');
                row['insuranceList.client.name'] = row.insuranceList.client.name;
                row['insuranceList.client.id_card'] = row.insuranceList.client.id_card;
                row['insuranceList.sn'] = row.insuranceList.sn;
                row['insuranceList.amount'] = row.insuranceList.amount;
                row['inputUser.username'] = row.inputUser.username;
                row['insuranceList.productItem.name'] = row.insuranceList.productItem.name;
                if (row.payMethod == 1){
                    row.payMethod = "现金支付";
                }else if (row.payMethod == 2){
                    row.payMethod = "刷卡支付"
                }else if (row.payMethod == 3){
                    row.payMethod = "微信支付"
                }
                emp_form.form('load', row);

                $.get("/insuranceTS/getCarproductByInsuId.do", {InsuId: row.insuranceList.id}, function (date) {
                    row.insuranceList.productItem.items = date;
                    row.products = row.insuranceList.productItem.items;
                    var ids = $.map(row.products, function (item) {
                        return item.name;
                    })
                    $("#selectedProduct").textbox('setValue', ids);
                })

                emp_dialog.dialog('setTitle', '查看详细信息');
                emp_dialog.dialog('open');
            }
        },

        intime:function(){

            if ($("#intime").linkbutton("options").iconCls=='icon-select'){
                $("#intime").linkbutton({
                    iconCls: 'icon-selected'
                })
                var keyword = $("#keyword").textbox('getValue');

                emp_list.datagrid('load', {
                    keyword: keyword,
                    current:true
                });
            }else if ($("#intime").linkbutton("options").iconCls=='icon-selected'){
                $("#intime").linkbutton({
                    iconCls: 'icon-select'
                })

                var keyword = $("#keyword").textbox('getValue');

                emp_list.datagrid('load', {
                    keyword: keyword,
                });
            }
        },

        query: function () {
            var keyword = $("#keyword").textbox('getValue');

            if ($("#intime").linkbutton("options").iconCls=='icon-select'){

                var keyword = $("#keyword").textbox('getValue');

                emp_list.datagrid('load', {
                    keyword: keyword,

                });
            }else if ($("#intime").linkbutton("options").iconCls=='icon-selected'){

                var keyword = $("#keyword").textbox('getValue');

                emp_list.datagrid('load', {
                    keyword: keyword,
                    current:true
                });
            }
        },

        cancel: function () {
            emp_dialog.dialog('close');
        }
    }
    /*事件统一绑定*/
    $("[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        object[cmd]();
    })


    emp_list.datagrid({
        view: detailview,
        detailFormatter:function(index,row){
            return '<table cellspacing="15" cellpadding="5" >'+
                '<tr>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">核保单号:</font> ' + row.insuranceList.sn +
                '</td>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">业务人员:</font> ' + (row.insuranceList.client.takeUser!=null?row.insuranceList.client.takeUser.realname:"暂无") + '</p>' +
                '</td>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">审核人员:</font> ' + row.insuranceList.verifyUser.realname + '</p>' +
                '</td>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">申请日期:</font> ' + row.insuranceList.inputtime + '</p>' +
                '</td>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">审核日期:</font> ' + row.insuranceList.verifytime + '</p>' +
                '</td>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">起保日期:</font> ' + row.beginDate + '</p>' +
                '</td>' +
                '</tr>'+
                '<tr>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">车主姓名:</font> ' + row.insuranceList.carInformation.name +
                '</td>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">车牌号码:</font> ' + row.insuranceList.carInformation.carNumber + '</p>' +
                '</td>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">车辆品牌:</font> ' + row.insuranceList.carInformation.brand + '</p>' +
                '</td>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">车辆类型:</font> ' + row.insuranceList.carInformation.type + '</p>' +
                '</td>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">车辆备注:</font> ' + (row.insuranceList.carInformation.remarks!=null?row.insuranceList.carInformation.remarks:"没有信息") + '</p>' +
                '</td>' +
                '<td style="border:0">' +
                '<p><font style="font-weight:bold">购置日期:</font> ' + row.insuranceList.carInformation.inputTime + '</p>' +
                '</td>' +
                '</tr>'+
                '</table>';
        },
        //onExpandRow: function(index,row){
        //    var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
        //    ddv.panel({
        //        height: 80,
        //        border: false,
        //        cache: false,
        //        //href: 'datagrid21_getdetail.php?itemid=' + row.itemid,
        //        onLoad: function () {
        //            $('#dg').datagrid('fixDetailRowHeight', index);
        //        }
        //    })
        //},
                //
        fit: true,
        //rownumbers: true,
        fitColumns: true,
        pagination: true,
        singleSelect: true,
        toolbar: '#emp_toolbar',
        url: '/paid/query.do',
        columns: [[
            {field: 'sn', title: '业务单号', width: 100},
            {field: 'insuranceList', title: '投保客户', width: 100, formatter: function (value, row, index) {
                return value ? value.client.name : '';
            }},
            {field: 'insuranceList.tel', title: '电话', width: 100, formatter: function (value, row, index) {
                return row.insuranceList.client.tel;
            }},
            {field: 'insuranceList.client.id_card', title: '身份证号', width: 100, formatter: function (value, row, index) {
                return row.insuranceList.client.id_card;
            }},
            {field: 'insuranceList.productItem.name', title: '承保机构', width: 100, formatter: function (value, row, index) {
                return row.insuranceList.productItem != null? row.insuranceList.productItem.name:'没有该机构';
            }},
            {field: 'endDate', title: '止保日期', width: 100},
            {field: 'inputUser', title: '负责人员', width: 100, formatter: function (value, row, index) {
                return value? value.username : '';
            }}
        ]]
    })

    emp_dialog.dialog({
        closed: true,
        width: 360,
        height: 470,
        buttons: $("#emp_diaButtons")
    })


})
